<?php
namespace app\crm\model;
use think\Model;
use think\model\concern\SoftDelete;

class CrmContact extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;
    protected $globalScope = ['shop_id'];
    
    public function scopeShop_id($query)
    {
        $query->where('shop_id', ShopId());
    }
    
    //关联保存联系人信息
    static function update_user($param,$customer_id) {
        //循环
        foreach ($param['type_id'] as $k=>$r){
            //如果有数据
            if(isset($param['id'][$k])){
                $data['type_id'] = $r;
                $data['name'] = $param['name'][$k];
                $data['mobile'] = $param['mobile'][$k];
                $data['email'] = $param['email'][$k];
                $data['wechat'] = $param['wechat'][$k];
                $data['status'] = '1';
                self::update($data,['id'=>$param['id'][$k]]);
            }else{
                $data['shop_id'] = ShopId();
                $data['customer_id'] = $customer_id;
                $data['type_id'] = $param['type_id'][$k];
                $data['name'] = $param['name'][$k];
                $data['mobile'] = $param['mobile'][$k];
                $data['email'] = $param['email'][$k];
                $data['wechat'] = $param['wechat'][$k];
                $data['status'] = '1';
                self::create($data);
            }
        }
        return true;
    }
    //关联分类
    public function type()
    {
        return $this->belongsTo(\app\user\model\DictData::class, 'type_id', 'id')->bind(['type_name'=>'name']);
    }
    
}